package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.PageEntity;
import com.cn.demo4.entity.UserEntity;
import com.cn.demo4.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * @author LXJ
 * date 2021-1-18
 */
@Controller
//地址映射
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private IUserService iUserService;
    @RequestMapping("/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, UserEntity userEntity,HttpServletRequest req) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String hobby = req.getParameter("hobby");
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        userEntity.setStatus(Tool.stringToInt(status));
        userEntity.setHobby(hobby);
        if(!hobby.equals("")){
            String[] hobbys = hobby.split(",");
            userEntity.setHobbys(Arrays.asList(hobbys));
        }
        PageEntity pageEntity = new PageEntity();
        pageEntity.setCurPage(Tool.toCurPage(req.getParameter("curPage")));
        pageEntity.setPageSize(Tool.toPageSize(req.getParameter("pageSize")));
        userEntity.setPageEntity(pageEntity);
        List<UserEntity> list = iUserService.findAll(userEntity);
        int count = iUserService.getCount(userEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp,UserEntity userEntity,HttpServletRequest req) throws  IOException{
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        UserEntity model =iUserService.findById(userEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/edit")
    @ResponseBody
    public void edit(HttpServletResponse resp,UserEntity userEntity) throws IOException{
        String result = iUserService.edit(userEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/editPassword")
    @ResponseBody
    public void editPassword(HttpServletResponse resp,UserEntity userEntity) throws IOException {
        String result = iUserService.editPassword(userEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/del")
    @ResponseBody
    public void del(HttpServletResponse resp,UserEntity userEntity) throws IOException {
        int result = iUserService.del(userEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }

}
